import Vue from 'vue'
import App from './App.vue'

import router from './router'

// 控制台提示 没有用
Vue.config.productionTip = false

// 引入全局样式
import './styles/base.less'
import './styles/iconfont.css'

// 引入全部组件
import HmHeader from './components/HmHeader.vue'
import HmLogo from './components/HmLogo.vue'
import HmButton from './components/HmButton.vue'
import HmPost from './components/HmPost.vue'
Vue.component('hm-post', HmPost)
Vue.component('hm-header', HmHeader)
Vue.component('hm-logo', HmLogo)
Vue.component('hm-button', HmButton)

// 引入
import 'lib-flexible'

// axios
import axios from 'axios'
axios.defaults.baseURL = 'http://localhost:3000'
Vue.prototype.$axios = axios

// 请求拦截器
axios.interceptors.request.use(config => {
  let token = localStorage.getItem('token')

  if (token) {
    config.headers.Authorization = token
  }

  return config
})

import { Toast } from 'vant'

// 相应拦截器
axios.interceptors.response.use(res => {
  const { statusCode, message } = res.data
  if (statusCode === 401 && message === '用户信息验证失败') {
    //1 跳转到login
    router.push('/login')

    //2. 移除
    localStorage.removeItem('token')
    localStorage.removeItem('user_id')

    //3. token失效
    Toast.fail('失效')
  }

  return res
})

// 引入 vant
import {
  Field,
  Cell,
  CellGroup,
  Dialog,
  Radio,
  RadioGroup,
  Uploader,
  List
} from 'vant'


Vue.use(List);
Vue.use(Uploader)
Vue.use(Field)
Vue.use(Cell)
Vue.use(CellGroup)
Vue.use(Toast)
Vue.use(Dialog)
Vue.use(Radio)
Vue.use(RadioGroup)

// 过滤器
import moment from 'moment'
Vue.filter('date', (res, format = 'YYYY-MM-DD') => {
  return moment(res).format(format)
})
Vue.filter('fixurl',(url)=>{
  if (url.startsWith("http")) {
    return url;
  } else {
    return axios.defaults.baseURL + url;
  }
})

// 主分支的内容
// 分支上的内容
new Vue({
        router,
        render: h => h(App)
      }).$mount('#app')
